<?php

namespace Tests;

use GuzzleHttp\Client;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected $token;

    public function getAuthHeader()
    {
        $response = $this->post('wx/auth/login', [
            'username' => 'yangming',
            'password' => '123456',
        ]);
        $token = $response->getOriginalContent()['data']['token'] ?? '';
        $this->token = $token;
        return ['Authorization' => "Bearer {$token}"];
    }

    public function assertLitemaillApiGet($url, $ignore = [])
    {
        return $this->assertLitemaillApi($url, 'get', [], $ignore);
    }

    public function assertLitemaillApiPost($url, $data = [], $ignore = [])
    {
        return $this->assertLitemaillApi($url, 'post', $data, $ignore);
    }

    public function assertLitemaillApi($url, $method = 'get', $data = [], $ignore = [])
    {
        $client = new Client();
        if ($method = 'get') {
            $response1 = $this->get($url, $this->getAuthHeader());
            $response2 = $client->get('http://www.mcshop.com/'.$url,
                ['headers' => ['Authorization' => "Bearer {$this->token}"]]
            );
        } else {
            $response1 = $this->post($url, $data, $this->getAuthHeader());
            $response2 = $client->post('http://www.mcshop.com/'.$url,
                [
                    'headers' => ['Authorization' => "Bearer {$this->token}"],
                    'json' => $data
                ]);
        }

        $content1 = $response1->getContent();
        echo $content1.PHP_EOL;
        $content1 = json_decode($content1, true);
        $content2 = $response2->getBody()->getContents();
        echo $content2;
        $content2 = json_decode($content2, true);
        foreach ($ignore as $key) {
            unset($content1[$key]);
            unset($content2[$key]);
        }
        $this->assertEquals($content2, $content1);
    }
}
